﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.Collections.ObjectModel;
using ELMApplication.Common.Const;
using System.Linq;
using ELMApplication.Common.Util;

namespace UserLoginModule.Views.AddMatterView
{
    public class AddMatterPrentationModel : IAddMatterPrentationModel
    {

         private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public AddMatterPrentationModel(IAddMatterView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;            
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
            this.View = view;           
            view.SetModel(this);
          //  initialData();
            this.ELMListData.DataLoadCompleted += new EventHandler(ELMListData_DataLoadCompleted);
            ELMListData.EditEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(hmList_EditEvent);
            ELMListData.DelEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(hmList_DelEvent);
            ELMListData.NewEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(ELMListData_NewEvent);
        }
        void ELMListData_NewEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            AddingMatter = new Matter();
            ShowDetial(PrivegeTypes.New, "");
        }
        void hmList_DelEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            MessageBoxResult res = MessageBox.Show("确定删除？", "", MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                delMatter(e.DataKey);
            }
        }

        void hmList_EditEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            // model.SetAddingHuman();
            ShowDetial(PrivegeTypes.Edit, e.DataKey);
        }

        void ELMListData_DataLoadCompleted(object sender, EventArgs e)
        {
            BusyMessage.Close();
        }
        private ContentType _extendInfo;
        public ContentType ExtendInfo
        {
            get
            {
                return _extendInfo;

            }
            set
            {
                _extendInfo = value;
                NotifyPropertyChanged("ExtendInfo");
            }
        }

        public void initialData()
        {
            BusyMessage.Show("数据读取中...");
            _domaincontext = new ELMServiceClient();
            ELMListData.LoadMatter(new Guid("49e9caaa-cc09-412b-87fc-92cc744304cc"));
            AddingMatter = new Matter();
            _domaincontext.GetContentTypeByIdAsync(new Guid("49e9caaa-cc09-412b-87fc-92cc744304cc"));
            _domaincontext.GetContentTypeByIdCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    ExtendInfo = data.Result;

                }

            };
            //_domaincontext.GetMattersAsync();
            //_domaincontext.GetMattersCompleted += (sender, data) =>
            //    {
            //        if(data.Error==null)
            //        {
            //            AllMatter=data.Result;
            //        }
            //    };
           
        }

       

        #region IAddUserPrentationModel Members

        public IAddMatterView View
        {
            get;
            set;
        }

        public void AddMatter()
        {
            if (!string.IsNullOrEmpty(AddingMatter.MatterId))
            {
                _domaincontext = new ELMServiceClient();
                _domaincontext.AddNewMatterAsync(AddingMatter);
                _domaincontext.AddNewMatterCompleted += (sender, data) =>
                    {
                        if (data.Error == null && data.Result)
                        {
                            initialData();
                            MessageBox.Show("添加成功");
                            AddingMatter = new Matter();
                        }
                        else
                        {
                            MessageBox.Show("添加失败");
                        }
                    };
            }
            BusyMessage.Close();
        }
        private Matter _addingMatter;
        public Matter AddingMatter
        {
            get
            {
                return _addingMatter;
            }
            set
            {
                _addingMatter = value;
                NotifyPropertyChanged("AddingMatter");
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion





        #region IAddMatterPrentationModel 成员

        private PrivegeTypes _detailPrivege;
        public PrivegeTypes DetailPrivege
        {
            get
            {
                return _detailPrivege;
            }
            set
            {
                _detailPrivege = value;
                NotifyPropertyChanged("DetailPrivege");
            }
        }

        #endregion

        #region IAddMatterPrentationModel 成员


        public void ShowDetial(PrivegeTypes privegeTypes, string DataKey)
        {
            DetailPrivege = privegeTypes;
            switch (privegeTypes)
            {
                case PrivegeTypes.New:
                    CW_Matter m = new CW_Matter(this);
                    m.Show();
                    break;
                default:
                    _domaincontext = new ELMServiceClient();
                    _domaincontext.GetMatterByIdAsync(DataKey);
                    _domaincontext.GetMatterByIdCompleted += (sender, data) =>
                    {
                        if (data.Error == null)
                        {
                            AddingMatter = data.Result;
                            //SelectedDuty = AllDuty.Single(c => c.DutyId == AddingHuman.DutyId);
                            m = new CW_Matter(this);
                            m.Show();
                        }
                        else
                        {
                            throw data.Error;
                        }
                    };
                    break;

            }
        }

        private ObservableCollection<Matter> _allMatter;
        public ObservableCollection<Matter> AllMatter
        {
            get
            {
                return _allMatter;
            }
            set
            {
                _allMatter = value;
                NotifyPropertyChanged("AllMatter");
            }
        }

        public void SetAddingMatter(string mId)
        {
            AddingMatter = AllMatter.Single(c=>c.MatterId==mId);
        }

        public void UpdateMatter()
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.UpdateMatterAsync(AddingMatter);
            _domaincontext.UpdateMatterCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        initialData();
                        BusyMessage.Close();
                    }
                };
        }

        public void delMatter(Matter mt)
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.DelMatterAsync(AddingMatter);
            _domaincontext.DelMatterCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    initialData();
                    BusyMessage.Close();
                }
            };
        }

        #endregion

        #region IAddMatterPrentationModel Members


        public ELMApplication.Common.UC.ELMListUC ELMListData
        {
            get;
            set;
        }

        #endregion

        #region IAddMatterPrentationModel Members


        public void delMatter(string mtId)
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.DelMatterByIdAsync(mtId);
            _domaincontext.DelMatterByIdCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    initialData();
                    BusyMessage.Close();
                }
            };
        }

        #endregion
    }
}
